package bat.ke.qq.com;

import bat.ke.qq.com.aop.AopMethodInterceptor;
import org.aopalliance.aop.Advice;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 *
 */
@Configuration
@ComponentScan("bat.ke.qq.com")
@EnableAspectJAutoProxy() // 开启AOP
public class AppConfig {

    @Bean
    public Advice aopMethodInterceptor() {
        return new AopMethodInterceptor();
    }

    /**
     * 通过 BeanNameAutoProxyCreator 指定 beanName 实现代理逻辑
     */
//	@Bean
//	public BeanNameAutoProxyCreator beanNameAutoProxyCreator(){
//        //使用BeanNameAutoProxyCreator来创建代理
//		BeanNameAutoProxyCreator beanNameAutoProxyCreator=new BeanNameAutoProxyCreator();
//		//设置要创建代理的 beanNames  类
//		beanNameAutoProxyCreator.setBeanNames("*Dao","*Service");
//		//设置拦截链名字(有先后顺序)   通知
//		beanNameAutoProxyCreator.setInterceptorNames("aopMethodInterceptor");
//		return beanNameAutoProxyCreator;
//	}

    /**
     * 根据Advisor的匹配机制自动创建代理，会对容器中所有的Advisor进行扫描，自动将这些切面应用到匹
     * 配的Bean中，实现类DefaultAdvisorAutoProxyCreator
     */
    @Bean
    public NameMatchMethodPointcutAdvisor nameMatchMethodPointcutAdvisor(){
        NameMatchMethodPointcutAdvisor nameMatchMethodPointcutAdvisor=
                new NameMatchMethodPointcutAdvisor();
        // 方法级别
        nameMatchMethodPointcutAdvisor.setMappedNames("query*","find*");
        nameMatchMethodPointcutAdvisor.setAdvice(aopMethodInterceptor());
        return nameMatchMethodPointcutAdvisor;
    }

//    @Bean
//    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
//        return new DefaultAdvisorAutoProxyCreator();
//    }


}
